﻿
// 2018/8/24: 首个记录
// 2018/11/29: 增加Protocols静态属性
// 2019/3/22: 时间戳赋值
// 2019/3/29: 添加注释
// 2020/1/14: 接口完备性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ASEva;

namespace FreespaceSampleV1
{
    class FreespaceSample : Sample
    {
        public List<PointF[]> Sequences { get; set; } // 可行驶区域顶点列表(可多段描述)，单位m

        public FreespaceSample()
        {
            Sequences = new List<PointF[]>();
        }

        public static string Title
        {
            get
            {
                return "Sequence count,First seq's offset, First seq's point count,etc";
            }
        }

        public static string Protocol
        {
            get
            {
                return "freespace-sample-v1";
            }
        }

        public static string[] Protocols
        {
            get
            {
                return new string[] { "freespace-sample-v1" };
            }
        }

        public override string GetGeneralSampleProtocol()
        {
            return Protocol;
        }

        public override string[] GetGeneralSampleProtocols()
        {
            return Protocols;
        }

        public override bool FromGeneralSample(GeneralSample sample)
        {
            if (sample.Protocol == "freespace-sample-v1")
            {
                var v = sample.Values.ToArray();
                if (v.Length < 1) return false;
                if (v[0].IsNotNumber()) return false;

                int seqCount = (int)v[0].number;
                if (v.Length < 1 + 2 * seqCount) return false;

                int seqBase = 1;
                int ptBase = seqBase + 2 * seqCount;

                var list = new List<PointF[]>();
                for (int i = 0; i < seqCount; i++)
                {
                    int ptOffset = (int)v[seqBase + 2 * i].number;
                    int ptCount = (int)v[seqBase + 2 * i + 1].number;
                    if (v.Length < ptBase + 2 * (ptOffset + ptCount)) return false;

                    var pts = new PointF[ptCount];
                    for (int n = 0; n < ptCount; n++)
                    {
                        pts[n].X = (float)v[ptBase + 2 * (ptOffset + n)].number;
                        pts[n].Y = (float)v[ptBase + 2 * (ptOffset + n) + 1].number;
                    }

                    list.Add(pts);
                }
                Sequences = list;

                Base = sample.Base;
                Offset = sample.Offset;
                Timeline = sample.Timeline;

                return true;
            }

            return false;
        }

    }
}
